package com.thinkfit.jy.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.common.lib.activity.BaseActivity;
import com.common.lib.bean.VideoBean;
import com.common.lib.manager.DataManager;
import com.thinkfit.jy.R;
import com.thinkfit.jy.adapter.TVAdapter;
import com.thinkfit.jy.contract.TVContract;
import com.thinkfit.jy.presenter.TVPresenter;
import com.thinkfit.jy.utils.DeviceTypeUtils;
import com.xiao.nicevideoplayer.NiceVideoPlayer;
import com.xiao.nicevideoplayer.NiceVideoPlayerManager;
import com.xiao.nicevideoplayer.TxVideoPlayerController;

import java.util.ArrayList;

/**
 * 视频课程
 */
public class TVActivity extends BaseActivity<TVContract.Presenter> implements TVContract.View {

    private TxVideoPlayerController mController;
    private NiceVideoPlayer mNiceVideoPlayer;

    private TVAdapter mAdapter;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_tv;
    }


    @Override
    protected void onCreated(@Nullable Bundle savedInstanceState) {
        if (DataManager.Companion.getInstance().getMainStyle() == 3) {
            setBackground(R.id.rootView, R.drawable.app_bg_xm);
            setBackground(R.id.ivHome, R.drawable.app_home_xm);
        }
        setViewsOnClickListener(R.id.ivMore, R.id.emptyView);
        getPresenter().getTVData();
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        LinearLayoutManager gridLayoutManager = new LinearLayoutManager(this);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(gridLayoutManager);
        getAdapter().onAttachedToRecyclerView(recyclerView);
        recyclerView.setAdapter(getAdapter());
    }


    @Override
    protected void onStop() {
        super.onStop();
        System.out.println("ddddddddddddddddddddddd   TVActivity ");
        DeviceTypeUtils.showBackButton(getApplicationContext());
    }

    private TVAdapter getAdapter() {
        if (mAdapter == null) {
            mAdapter = new TVAdapter(this);
            mAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                    showVideo(mAdapter.getItem(position));
                    getAdapter().resetIndex(position);
                    setViewGone(R.id.ll);
                    setViewVisible(R.id.ivMore);
                }
            });
        }
        return mAdapter;
    }


    @NonNull
    @Override
    protected TVContract.Presenter onCreatePresenter() {
        return new TVPresenter(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivMore:
                setViewVisible(R.id.ll);
                setViewGone(R.id.ivMore);
                break;
            case R.id.emptyView:
                setViewGone(R.id.ll);
                setViewVisible(R.id.ivMore);
                break;
        }
    }

    private void showVideo(VideoBean bean) {
        NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
        getNiceVideoPlayer().setPlayerType(NiceVideoPlayer.TYPE_IJK);
        if (mController == null) {
            mController = new TxVideoPlayerController(this);
            mController.setTitle("");
            getNiceVideoPlayer().setController(mController);
        }
        if (!TextUtils.isEmpty(bean.getUrl())) {
            getNiceVideoPlayer().setUp(bean.getUrl(), null);//"http" + Objects.requireNonNull(mVideo.getUrl()).substring(5), null);
        }
        mController.imageView().setImageResource(0);
        mController.imageView().setVisibility(View.GONE);
    }

    public void onDestroy() {
        super.onDestroy();
        NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
    }

    private NiceVideoPlayer getNiceVideoPlayer() {
        if (mNiceVideoPlayer == null) {
            mNiceVideoPlayer = findViewById(R.id.videoView);
        }
        return mNiceVideoPlayer;
    }

    @Override
    public void getTVDataSuccess(ArrayList<VideoBean> list) {
        if (isFinish() && list != null && !list.isEmpty()) {
            return;
        }
        showVideo(list.get(0));
        getAdapter().setNewInstance(list);
    }
}